<template><div><p>本文档最新版为 <a href="https://learnku.com/docs/laravel/10.x" target="_blank" rel="noopener noreferrer">10.x</a>，旧版本可能放弃维护，推荐阅读最新版！</p>
<h2 id="发送邮件" tabindex="-1"><a class="header-anchor" href="#发送邮件"><span>发送邮件</span></a></h2>
<ul>
<li><a href="#introduction">介绍</a>
<ul>
<li><a href="#configuration">配置</a></li>
<li><a href="#driver-prerequisites">驱动前提</a></li>
<li><a href="#failover-configuration">故障转移配置</a></li>
<li><a href="#round-robin-configuration">轮询调度配置</a></li>
</ul>
</li>
<li><a href="#generating-mailables">生成 Mailables</a></li>
<li><a href="#writing-mailables">编写 Mailables</a>
<ul>
<li><a href="#configuring-the-sender">配置发件人</a></li>
<li><a href="#configuring-the-view">配置邮件视图</a></li>
<li><a href="#view-data">视图数据</a></li>
<li><a href="#attachments">附件</a></li>
<li><a href="#inline-attachments">内联附件</a></li>
<li><a href="#attachable-objects">可附着对象</a></li>
<li><a href="#headers">标头</a></li>
<li><a href="#tags-and-metadata">标签及元数据</a></li>
<li><a href="#customizing-the-symfony-message">自定义 Symfony 消息</a></li>
</ul>
</li>
<li><a href="#markdown-mailables">Markdown 邮件</a>
<ul>
<li><a href="#generating-markdown-mailables">生成 Markdown 邮件</a></li>
<li><a href="#writing-markdown-messages">编写 Markdown 消息</a></li>
<li><a href="#customizing-the-components">自定义组件</a></li>
</ul>
</li>
<li><a href="#sending-mail">发送邮件</a>
<ul>
<li><a href="#queueing-mail">邮件队列</a></li>
</ul>
</li>
<li><a href="#rendering-mailables">渲染邮件</a>
<ul>
<li><a href="#previewing-mailables-in-the-browser">浏览器中预览邮件</a></li>
</ul>
</li>
<li><a href="#localizing-mailables">本地化邮件</a></li>
<li><a href="#testing-mailables">测试</a>
<ul>
<li><a href="#testing-mailable-content">测试邮件内容</a></li>
<li><a href="#testing-mailable-sending">测试邮件的发送</a></li>
</ul>
</li>
<li><a href="#mail-and-local-development">邮件及本地开发</a></li>
<li><a href="#events">事件</a></li>
<li><a href="#custom-transports">自定义传输</a>
<ul>
<li><a href="#additional-symfony-transports">额外的 Symfony 传输</a></li>
</ul>
</li>
</ul>
<h2 id="介绍" tabindex="-1"><a class="header-anchor" href="#介绍"><span>介绍</span></a></h2>
<p>发送邮件并不复杂。Laravel 基于 <a href="https://symfony.com/doc/7.0/mailer.html" target="_blank" rel="noopener noreferrer">Symfony Mailer</a> 组件提供了一个简洁、简单的邮件 API。Laravel 和 Symfony Mailer 为通过 SMTP、Mailgun、Postmark、Resend、Amazon SES、 及 <code v-pre>sendmail</code> 等方式发送邮件提供了驱动，允许你通过本地或者云服务来快速发送邮件。</p>
<h3 id="配置" tabindex="-1"><a class="header-anchor" href="#配置"><span>配置</span></a></h3>
<p>Laravel 的邮件系统可以通过应用的 <code v-pre>config/mail.php</code> 配置文件进行配置。在该文件中配置的每个邮件程序都可能有自己独特的配置，甚至有自己唯一的「传输方式」，允许你的应用使用不同的电子邮件服务发送某些电子邮件。例如，你的应用可能在使用 Amazon SES 批量发送邮件的同时，使用 Postmark 发送事务性邮件。</p>
<p>在 <code v-pre>mail</code> 配置文件中，你可以看到一个 <code v-pre>mailers</code> 配置数组。此数组包含 Laravel 支持的每个主要邮件驱动程序/传输的示例配置条目，而 <code v-pre>defalut</code> 配置值决定了当应用需要发送电子邮件时，默认情况下将使用哪个邮件程序。</p>
<h3 id="驱动-传输的前提" tabindex="-1"><a class="header-anchor" href="#驱动-传输的前提"><span>驱动/传输的前提</span></a></h3>
<p>基于 API 的驱动，比如 Mailgun、Postmark、Resend 和 MailerSend，通常比 SMTP 服务器更简单快速。如果可以的话，我们推荐你使用下面这些驱动。</p>
<h4 id="mailgun-驱动" tabindex="-1"><a class="header-anchor" href="#mailgun-驱动"><span>Mailgun 驱动</span></a></h4>
<p>要使用 Mailgun 驱动，请通过 Composer 安装 Symfony 的 Mailgun Mailer 传输库：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line"><span class="token function">composer</span> require symfony/mailgun-mailer symfony/http-client</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>接下来，在应用的 <code v-pre>config/mail.php</code> 配置文件中将 <code v-pre>default</code> 选项配置为 <code v-pre>mailgun</code>，并将如下配置数组添加到 <code v-pre>mailers</code> 数组中：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'mailgun'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'transport'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'mailgun'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token comment">// 'client' => [</span></span>
<span class="line">    <span class="token comment">//     'timeout' => 5,</span></span>
<span class="line">    <span class="token comment">// ],</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>在配置完应用默认的邮件程序后，请将如下选项添加到 <code v-pre>config/services.php</code> 配置文件：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'mailgun'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'domain'</span> <span class="token operator">=></span> <span class="token function">env</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'MAILGUN_DOMAIN'</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'secret'</span> <span class="token operator">=></span> <span class="token function">env</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'MAILGUN_SECRET'</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'endpoint'</span> <span class="token operator">=></span> <span class="token function">env</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'MAILGUN_ENDPOINT'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'api.mailgun.net'</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'scheme'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'https'</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果使用的不是 US <a href="https://documentation.mailgun.com/en/latest/api-intro.html#mailgun-regions" target="_blank" rel="noopener noreferrer">Mailgun region</a> 区域终端 ，你需要在 <code v-pre>services</code> 配置文件中配置区域终端：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'mailgun'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'domain'</span> <span class="token operator">=></span> <span class="token function">env</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'MAILGUN_DOMAIN'</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'secret'</span> <span class="token operator">=></span> <span class="token function">env</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'MAILGUN_SECRET'</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'endpoint'</span> <span class="token operator">=></span> <span class="token function">env</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'MAILGUN_ENDPOINT'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'api.eu.mailgun.net'</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'scheme'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'https'</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="postmark-驱动" tabindex="-1"><a class="header-anchor" href="#postmark-驱动"><span>Postmark 驱动</span></a></h4>
<p>要使用 <a href="https://postmarkapp.com/" target="_blank" rel="noopener noreferrer">Postmark</a> 驱动，请通过 Composer 安装 Symfony 的 PostMark Mailer 传输库：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line"><span class="token function">composer</span> require symfony/postmark-mailer symfony/http-client</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>接下来，在应用程序的 <code v-pre>config/mail.php</code> 配置文件中将 <code v-pre>default</code> 选项设置为 <code v-pre>postmark</code>。配置应用程序的默认邮件程序后，确保您的 <code v-pre>config/services.php</code> 配置文件包含以下选项：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'postmark'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'token'</span> <span class="token operator">=></span> <span class="token function">env</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'POSTMARK_TOKEN'</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果您想指定特定邮件程序应使用的 Postmark 消息流，可以在邮件程序的配置数组中添加 <code v-pre>message_stream_id</code> 配置选项。这个配置数组可以在应用程序的 <code v-pre>config/mail.php</code> 配置文件中找到：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'postmark'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'transport'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'postmark'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'message_stream_id'</span> <span class="token operator">=></span> <span class="token function">env</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'POSTMARK_MESSAGE_STREAM_ID'</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token comment">// 'client' => [</span></span>
<span class="line">    <span class="token comment">//     'timeout' => 5,</span></span>
<span class="line">    <span class="token comment">// ],</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>这样，您也可以设置多个具有不同消息流的 Postmark 邮件程序。</p>
<h4 id="resend-驱动程序" tabindex="-1"><a class="header-anchor" href="#resend-驱动程序"><span>Resend 驱动程序</span></a></h4>
<p>要使用 <a href="https://resend.com/" target="_blank" rel="noopener noreferrer">Resend</a> 驱动程序，请通过 Composer 安装 Resend 的 PHP SDK：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line"><span class="token function">composer</span> require resend/resend-php</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>接下来，在应用程序的 <code v-pre>config/mail.php</code> 配置文件中将 <code v-pre>default</code> 选项设置为 <code v-pre>resend</code>。配置应用程序的默认邮件程序后，确保您的 <code v-pre>config/services.php</code> 配置文件包含以下选项：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'resend'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'key'</span> <span class="token operator">=></span> <span class="token function">env</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'RESEND_KEY'</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="ses-驱动程序" tabindex="-1"><a class="header-anchor" href="#ses-驱动程序"><span>SES 驱动程序</span></a></h4>
<p>要使用 Amazon SES 驱动程序，您必须首先安装 Amazon AWS SDK for PHP。您可以通过 Composer 包管理器安装此库：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line"><span class="token function">composer</span> require aws/aws-sdk-php</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>接下来，在 <code v-pre>config/mail.php</code> 配置文件中将 <code v-pre>default</code> 选项设置为 <code v-pre>ses</code> ，并确保您的 <code v-pre>config/services.php</code> 配置文件包含以下选项：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'ses'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'key'</span> <span class="token operator">=></span> <span class="token function">env</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'AWS_ACCESS_KEY_ID'</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'secret'</span> <span class="token operator">=></span> <span class="token function">env</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'AWS_SECRET_ACCESS_KEY'</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'region'</span> <span class="token operator">=></span> <span class="token function">env</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'AWS_DEFAULT_REGION'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'us-east-1'</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>为了通过 session token 来使用 AWS <a href="https://docs.aws.amazon.com/IAM/latest/UserGuide/id_credentials_temp_use-resources.html" target="_blank" rel="noopener noreferrer">temporary credentials</a>，你需要向应用的 SES 配置中添加一个 <code v-pre>token</code> 键：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'ses'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'key'</span> <span class="token operator">=></span> <span class="token function">env</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'AWS_ACCESS_KEY_ID'</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'secret'</span> <span class="token operator">=></span> <span class="token function">env</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'AWS_SECRET_ACCESS_KEY'</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'region'</span> <span class="token operator">=></span> <span class="token function">env</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'AWS_DEFAULT_REGION'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'us-east-1'</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'token'</span> <span class="token operator">=></span> <span class="token function">env</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'AWS_SESSION_TOKEN'</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>要与SES的<a href="https://docs.aws.amazon.com/ses/latest/dg/sending-email-subscription-management.html" target="_blank" rel="noopener noreferrer">订阅管理功能</a> 交互，您可以在邮件消息的<a href="#headers"><code v-pre>headers</code></a> 方法返回的数组中返回 <code v-pre>X-Ses-List-Management-Options</code> 请求头：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * Get the message headers.</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">headers</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token class-name return-type">Headers</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">Headers</span><span class="token punctuation">(</span></span>
<span class="line">        <span class="token argument-name">text</span><span class="token punctuation">:</span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'X-Ses-List-Management-Options'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'contactListName=MyContactList;topicName=MyTopic'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>在发送电子邮件时，如果你想让 Laravel 传递<a href="https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-sesv2-2019-09-27.html#sendemail" target="_blank" rel="noopener noreferrer">附加选项</a>给 AWS SDK 的 <code v-pre>SendEmail</code> 方法，您可以在 <code v-pre>ses</code> 配置中定义一个 <code v-pre>options</code> 数组：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'ses'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'key'</span> <span class="token operator">=></span> <span class="token function">env</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'AWS_ACCESS_KEY_ID'</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'secret'</span> <span class="token operator">=></span> <span class="token function">env</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'AWS_SECRET_ACCESS_KEY'</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'region'</span> <span class="token operator">=></span> <span class="token function">env</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'AWS_DEFAULT_REGION'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'us-east-1'</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'options'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'ConfigurationSetName'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'MyConfigurationSet'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'EmailTags'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token punctuation">[</span><span class="token string single-quoted-string">'Name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'foo'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Value'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'bar'</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="mailersend-驱动程序" tabindex="-1"><a class="header-anchor" href="#mailersend-驱动程序"><span>MailerSend 驱动程序</span></a></h4>
<p><a href="https://www.mailersend.com/" target="_blank" rel="noopener noreferrer">MailerSend</a>，一个事务性电子邮件和 SMS 服务，为 Laravel 维护自己的基于 API 的邮件驱动程序。可以通过 Composer 包管理器安装包含这个驱动程序的包：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line"><span class="token function">composer</span> require mailersend/laravel-driver</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>Once the package is installed, add the <code v-pre>MAILERSEND_API_KEY</code> environment variable to your application's <code v-pre>.env</code> file. In addition, the <code v-pre>MAIL_MAILER</code> environment variable should be defined as <code v-pre>mailersend</code>:<br>
安装扩展包后，需要将 <code v-pre>MAILERSEND_API_KEY</code> 环境变量添加到应用程序的 <code v-pre>.env</code> 文件中。另外，<code v-pre>MAIL_MAILER</code> 的环境变量应定义为 <code v-pre>mailersend</code> ：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line"><span class="token assign-left variable">MAIL_MAILER</span><span class="token operator">=</span>mailersend</span>
<span class="line"><span class="token assign-left variable">MAIL_FROM_ADDRESS</span><span class="token operator">=</span>app@yourdomain.com</span>
<span class="line"><span class="token assign-left variable">MAIL_FROM_NAME</span><span class="token operator">=</span><span class="token string">"App Name"</span></span>
<span class="line"></span>
<span class="line"><span class="token assign-left variable">MAILERSEND_API_KEY</span><span class="token operator">=</span>your-api-key</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>最后，将 MailerSend 添加到应用程序的 <code v-pre>config/mail.php</code> 配置文件中的 <code v-pre>mailers</code> 数组中</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'mailersend'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'transport'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'mailersend'</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>要了解有关 MailerSend 的更多信息，包括如何使用托管模板， 请参阅 <a href="https://github.com/mailersend/mailersend-laravel-driver#usage" target="_blank" rel="noopener noreferrer">MailerSend 驱动文档</a>。</p>
<h3 id="故障转移配置" tabindex="-1"><a class="header-anchor" href="#故障转移配置"><span>故障转移配置</span></a></h3>
<p>有时，已经配置好用于发送应用程序邮件的外部服务可能会关闭。在这种情况下，定义一个或多个备用邮件发送配置可能非常有用，这些配置将在你的主发送驱动程序关闭时使用。</p>
<p>因此，你应该在应用程序的 <code v-pre>mail</code> 配置文件中定义一个使用 <code v-pre>failover</code> 传输的邮件程序。应用程序的<code v-pre>failover</code> 邮件程序的配置数组应包含一个 <code v-pre>mailers</code> 数组，该数组引用应选择配置邮件程序进行传递的顺序：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'mailers'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'failover'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'transport'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'failover'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'mailers'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'postmark'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'mailgun'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'sendmail'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// ...</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>定义故障转移邮件程序后，你应该将此邮件程序设置为应用程序使用的默认邮件程序，方法是将其名称指定为应用程序 <code v-pre>mail</code> 配置文件中 <code v-pre>default</code> 配置键的值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'default'</span> <span class="token operator">=></span> <span class="token function">env</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'MAIL_MAILER'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'failover'</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="轮询调度配置" tabindex="-1"><a class="header-anchor" href="#轮询调度配置"><span>轮询调度配置</span></a></h3>
<p><code v-pre>roundrobin</code> 传输允许你将邮件工作负载分布在多个邮件程序中。首先，要在应用程序的 <code v-pre>mail</code> 配置文件中定义一个使用 <code v-pre>roundrobin</code> 传输的邮件程序。应用程序的 <code v-pre>roundrobin</code> 邮件程序的配置数组应包含一个 <code v-pre>mailers</code> 数组，该数组引用应使用哪些配置的邮件程序进行传递：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'mailers'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'roundrobin'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'transport'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'roundrobin'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'mailers'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'ses'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'postmark'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// ...</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>在成功配置了循环轮询邮件发送程序后，你应当将其设置为默认的邮件发送器。这一步骤简单而关键，只需在应用的 <code v-pre>mail</code> 配置文件中，将 <code v-pre>default</code> 键对应的值更新为你所创建的循环轮询邮件发送器的名称即可。</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'default'</span> <span class="token operator">=></span> <span class="token function">env</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'MAIL_MAILER'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'roundrobin'</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>循环轮询传输机制从已配置的邮件发送器列表中随机选取一个进行邮件发送，并在后续的每一封邮件发送中切换至下一个可用的邮件发送器。与 <code v-pre>failover</code>传输对比。<code v-pre>failover</code> 旨在实现 <a href="https://en.wikipedia.org/wiki/High_availability" target="_blank" rel="noopener noreferrer">高可用性</a>，而<code v-pre>roundrobin</code> 传输则提供<a href="https://en.wikipedia.org/wiki/Load_balancing_(computing)" target="_blank" rel="noopener noreferrer">负载平衡</a>。</p>
<h2 id="生成-mailables" tabindex="-1"><a class="header-anchor" href="#生成-mailables"><span>生成 Mailables</span></a></h2>
<p>在构建 Laravel 应用程序时，应用程序发送的每种类型的电子邮件都表示为一个 <code v-pre>mailable</code> 类。这些类存储在 <code v-pre>app/Mail</code> 目录中。如果你在应用程序中看不到此目录，请不要担心，因为在你使用 <code v-pre>make:mail</code> Artisan 命令创建第一个 mailable 类时，它将自动生成：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">php artisan make:mail OrderShipped</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h2 id="编写-mailables" tabindex="-1"><a class="header-anchor" href="#编写-mailables"><span>编写 Mailables</span></a></h2>
<p>生成 mailable 类后，就打开它，这样我们就可以探索它的内容。邮件类的配置可以通过多种方法完成，包括 <code v-pre>envelope</code>、<code v-pre>content</code> 和 <code v-pre>attachments</code> 方法。</p>
<p><code v-pre>envelope</code> 方法返回一个<code v-pre>Illuminate\Mail\Mailables\Envelope</code>对象，用于定义消息的主题，有时还定义邮件的收件人。<code v-pre>content</code> 方法返回一个<code v-pre>Illuminate\Mail\Mailables\Content</code>对象，用于定义将用于生成消息内容的 <a href="https://learnku.com/docs/laravel/11.x/blademd" target="_blank" rel="noopener noreferrer">Blade 模板</a>。</p>
<h3 id="配置发件人" tabindex="-1"><a class="header-anchor" href="#配置发件人"><span>配置发件人</span></a></h3>
<h4 id="使用-envelope" tabindex="-1"><a class="header-anchor" href="#使用-envelope"><span>使用 Envelope</span></a></h4>
<p>首先，让我们来看下如何配置电子邮件的发件人。电子邮件的「发件人」。有两种方法可以配置发送者。首先，你可以在邮件信封上指定「发件人」地址：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Mail<span class="token punctuation">\</span>Mailables<span class="token punctuation">\</span>Address</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Mail<span class="token punctuation">\</span>Mailables<span class="token punctuation">\</span>Envelope</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 获取邮件信封。</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">envelope</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token class-name return-type">Envelope</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">Envelope</span><span class="token punctuation">(</span></span>
<span class="line">        <span class="token argument-name">from</span><span class="token punctuation">:</span> <span class="token keyword">new</span> <span class="token class-name">Address</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'jeffrey@example.com'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Jeffrey Way'</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token argument-name">subject</span><span class="token punctuation">:</span> <span class="token string single-quoted-string">'订单发货'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>除此之外，还可以指定 <code v-pre>replyTo</code> 地址：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">Envelope</span><span class="token punctuation">(</span></span>
<span class="line">    <span class="token argument-name">from</span><span class="token punctuation">:</span> <span class="token keyword">new</span> <span class="token class-name">Address</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'jeffrey@example.com'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Jeffrey Way'</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token argument-name">replyTo</span><span class="token punctuation">:</span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token keyword">new</span> <span class="token class-name">Address</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'taylor@example.com'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Taylor Otwell'</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token argument-name">subject</span><span class="token punctuation">:</span> <span class="token string single-quoted-string">'订单发货'</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="使用全局-from-地址" tabindex="-1"><a class="header-anchor" href="#使用全局-from-地址"><span>使用全局 <code v-pre>from</code> 地址</span></a></h4>
<p>当然，如果你的应用在任何邮件中使用的「发件人」地址都一致的话，在你生成的每一个 mailable 类中调用 <code v-pre>from</code> 方法可能会很麻烦。因此，你可以在 <code v-pre>config/mail.php</code> 文件中指定一个全局的「发件人」地址。当某个 mailable 类没有指定「发件人」时，它将使用该全局「发件人」：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'from'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'address'</span> <span class="token operator">=></span> <span class="token function">env</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'MAIL_FROM_ADDRESS'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'hello@example.com'</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token function">env</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'MAIL_FROM_NAME'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Example'</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>此外，你可以在 <code v-pre>config/mail.php</code> 配置文件中定义全局 「reply_to」 地址：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'reply_to'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'address'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'example@example.com'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'App Name'</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="配置视图" tabindex="-1"><a class="header-anchor" href="#配置视图"><span>配置视图</span></a></h3>
<p>在邮件类下的 <code v-pre>content</code> 方法中使用 <code v-pre>view</code> 方法来指定在渲染邮件内容时要使用的模板。由于每封电子邮件通常使用一个 <a href="https://learnku.com/docs/laravel/11.x/blade" target="_blank" rel="noopener noreferrer">Blade 模板</a> 来渲染其内容。因此在构建电子邮件的 HTML 时，可以充分利用 Blade 模板引擎的功能和便利性：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 获取消息内容定义。</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">content</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token class-name return-type">Content</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">Content</span><span class="token punctuation">(</span></span>
<span class="line">        <span class="token argument-name">view</span><span class="token punctuation">:</span> <span class="token string single-quoted-string">'mail.orders.shipped'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p>[技巧]<br>
你可以创建一个 <code v-pre>resources/views/emails</code> 目录来存放所有的邮件模板；当然，也可以将其置于 <code v-pre>resources/views</code> 目录下的任何位置。</p>
</blockquote>
<h4 id="纯文本邮件" tabindex="-1"><a class="header-anchor" href="#纯文本邮件"><span>纯文本邮件</span></a></h4>
<p>如果你想定义电子邮件的纯文本版本，你可以在创建邮件的 <code v-pre>Content</code> 定义时指定纯文本模板。与 <code v-pre>view</code> 参数一样， <code v-pre>text</code> 参数应该是用于呈现电子邮件内容的模板名称。你可以自由定义邮件的 HTML 和纯文本版本：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 获取消息内容定义</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">content</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token class-name return-type">Content</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">Content</span><span class="token punctuation">(</span></span>
<span class="line">        <span class="token argument-name">view</span><span class="token punctuation">:</span> <span class="token string single-quoted-string">'mail.orders.shipped'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token argument-name">text</span><span class="token punctuation">:</span> <span class="token string single-quoted-string">'mail.orders.shipped-text'</span></span>
<span class="line">    <span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>为清楚起见， <code v-pre>html</code> 参数可用作 <code v-pre>view</code> 参数的别名：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">Content</span><span class="token punctuation">(</span></span>
<span class="line">    <span class="token argument-name">html</span><span class="token punctuation">:</span> <span class="token string single-quoted-string">'mail.orders.shipped'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token argument-name">text</span><span class="token punctuation">:</span> <span class="token string single-quoted-string">'mail.orders.shipped-text'</span></span>
<span class="line"><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="视图数据" tabindex="-1"><a class="header-anchor" href="#视图数据"><span>视图数据</span></a></h3>
<h4 id="通过-public-属性" tabindex="-1"><a class="header-anchor" href="#通过-public-属性"><span>通过 Public 属性</span></a></h4>
<p>通常，你希望将一些数据传递给视图，以便在呈现电子邮件的 HTML 时使用这些数据。有两种方法可以使数据对视图可用。首先，在 mailable 类上定义的任何公共属性都将自动对视图可用。例如，你可以将数据传递到 mailable 类的构造函数中，并将该数据设置为在类上定义的公共属性：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">namespace</span> <span class="token package">App<span class="token punctuation">\</span>Mail</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Models<span class="token punctuation">\</span>Order</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Bus<span class="token punctuation">\</span>Queueable</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Mail<span class="token punctuation">\</span>Mailable</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Mail<span class="token punctuation">\</span>Mailables<span class="token punctuation">\</span>Content</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Queue<span class="token punctuation">\</span>SerializesModels</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">OrderShipped</span> <span class="token keyword">extends</span> <span class="token class-name">Mailable</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">use</span> <span class="token package">Queueable</span><span class="token punctuation">,</span> SerializesModels<span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * 创建新的消息实例</span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">__construct</span><span class="token punctuation">(</span></span>
<span class="line">        <span class="token keyword">public</span> <span class="token class-name type-declaration">Order</span> <span class="token variable">$order</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">)</span> <span class="token punctuation">{</span><span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * 获取消息内容定义</span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">content</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token class-name return-type">Content</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">Content</span><span class="token punctuation">(</span></span>
<span class="line">            <span class="token argument-name">view</span><span class="token punctuation">:</span> <span class="token string single-quoted-string">'mail.orders.shipped'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>将数据设置为公共属性，它将自动在视图中可用，因此你可以像访问 Blade 模板中的任何其他数据一样访问它：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token operator">&lt;</span>div<span class="token operator">></span></span>
<span class="line">    Price<span class="token punctuation">:</span> <span class="token punctuation">{</span><span class="token punctuation">{</span> <span class="token variable">$order</span><span class="token operator">-></span><span class="token property">price</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></span>
<span class="line"><span class="token operator">&lt;</span><span class="token operator">/</span>div<span class="token operator">></span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="通过-with-参数" tabindex="-1"><a class="header-anchor" href="#通过-with-参数"><span>通过 <code v-pre>with</code> 参数：</span></a></h4>
<p>如果你想要在邮件数据发送到模板前自定义它们的格式，你可以通过 <code v-pre>Content</code> 定义的 <code v-pre>with</code> 参数来手动传递数据到视图中。通常，你还是会通过 mailable 类的构造函数来传递数据；但是，你应该把这些数据设置为 <code v-pre>protected</code> 或 <code v-pre>private</code> 属性，这样数据就不会自动提供给模板：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">namespace</span> <span class="token package">App<span class="token punctuation">\</span>Mail</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Models<span class="token punctuation">\</span>Order</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Bus<span class="token punctuation">\</span>Queueable</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Mail<span class="token punctuation">\</span>Mailable</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Mail<span class="token punctuation">\</span>Mailables<span class="token punctuation">\</span>Content</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Queue<span class="token punctuation">\</span>SerializesModels</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">OrderShipped</span> <span class="token keyword">extends</span> <span class="token class-name">Mailable</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">use</span> <span class="token package">Queueable</span><span class="token punctuation">,</span> SerializesModels<span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * 创建新的消息实例</span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">__construct</span><span class="token punctuation">(</span></span>
<span class="line">        <span class="token keyword">protected</span> <span class="token class-name type-declaration">Order</span> <span class="token variable">$order</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">)</span> <span class="token punctuation">{</span><span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * 获取消息内容定义</span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">content</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token class-name return-type">Content</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">Content</span><span class="token punctuation">(</span></span>
<span class="line">            <span class="token argument-name">view</span><span class="token punctuation">:</span> <span class="token string single-quoted-string">'mail.orders.shipped'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token argument-name">with</span><span class="token punctuation">:</span> <span class="token punctuation">[</span></span>
<span class="line">                <span class="token string single-quoted-string">'orderName'</span> <span class="token operator">=></span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token property">order</span><span class="token operator">-></span><span class="token property">name</span><span class="token punctuation">,</span></span>
<span class="line">                <span class="token string single-quoted-string">'orderPrice'</span> <span class="token operator">=></span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token property">order</span><span class="token operator">-></span><span class="token property">price</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>一旦数据被传递到 <code v-pre>with</code> 方法，它将自动在你的视图中可用，因此你可以像访问 Blade 模板中的任何其他数据一样访问它：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token operator">&lt;</span>div<span class="token operator">></span></span>
<span class="line">    Price<span class="token punctuation">:</span> <span class="token punctuation">{</span><span class="token punctuation">{</span> <span class="token variable">$orderPrice</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></span>
<span class="line"><span class="token operator">&lt;</span><span class="token operator">/</span>div<span class="token operator">></span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="附件" tabindex="-1"><a class="header-anchor" href="#附件"><span>附件</span></a></h3>
<p>要向电子邮件添加附件，你应该向邮件的<code v-pre>attachments</code> 方法返回的数组添加附件。首先，可以通过向 <code v-pre>Attachment</code> 类提供的 <code v-pre>fromPath</code> 方法提供文件路径来添加附件：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Mail<span class="token punctuation">\</span>Mailables<span class="token punctuation">\</span>Attachment</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 获取邮件的附件</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@return</span> <span class="token class-name"><span class="token keyword">array</span></span>&lt;int, \Illuminate\Mail\Mailables\Attachment></span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">attachments</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">array</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token class-name static-context">Attachment</span><span class="token operator">::</span><span class="token function">fromPath</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/path/to/file'</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>将文件附加到邮件时，你还可以使用 <code v-pre>as</code> 和 <code v-pre>withMime</code> 方法指定附件的显示名称和 / 或 MIME 类型：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 获取邮件的附件</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@return</span> <span class="token class-name"><span class="token keyword">array</span></span>&lt;int, \Illuminate\Mail\Mailables\Attachment></span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">attachments</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">array</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token class-name static-context">Attachment</span><span class="token operator">::</span><span class="token function">fromPath</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/path/to/file'</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">as</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name.pdf'</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">withMime</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'application/pdf'</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="从磁盘中添加附件" tabindex="-1"><a class="header-anchor" href="#从磁盘中添加附件"><span>从磁盘中添加附件</span></a></h4>
<p>如果你已经在 <a href="https://learnku.com/docs/laravel/11.x/filesystem" target="_blank" rel="noopener noreferrer">文件系统磁盘</a> 之一上存储了一个文件，则可以使用 <code v-pre>fromStorage</code> 附件方法将其添加到邮件中：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 获取邮件的附件</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@return</span> <span class="token class-name"><span class="token keyword">array</span></span>&lt;int, \Illuminate\Mail\Mailables\Attachment></span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">attachments</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">array</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token class-name static-context">Attachment</span><span class="token operator">::</span><span class="token function">fromStorage</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/path/to/file'</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>当然，你也可以指定附件的名称和 MIME 类型：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 获取邮件的附件</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@return</span> <span class="token class-name"><span class="token keyword">array</span></span>&lt;int, \Illuminate\Mail\Mailables\Attachment></span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">attachments</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">array</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token class-name static-context">Attachment</span><span class="token operator">::</span><span class="token function">fromStorage</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/path/to/file'</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">as</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name.pdf'</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">withMime</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'application/pdf'</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果你需要指定默认磁盘以外的存储磁盘，可以使用 <code v-pre>fromStorageDisk</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 获取邮件的附件</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@return</span> <span class="token class-name"><span class="token keyword">array</span></span>&lt;int, \Illuminate\Mail\Mailables\Attachment></span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">attachments</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">array</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token class-name static-context">Attachment</span><span class="token operator">::</span><span class="token function">fromStorageDisk</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'s3'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'/path/to/file'</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">as</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name.pdf'</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">withMime</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'application/pdf'</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="原始数据附件" tabindex="-1"><a class="header-anchor" href="#原始数据附件"><span>原始数据附件</span></a></h4>
<p><code v-pre>fromData</code> 附件方法可用于将原始字节字符串作为附件附加。例如，如果你在内存中生成了PDF，并且希望将其附加到电子邮件而不将其写入磁盘，则可以使用此方法。 <code v-pre>fromData</code> 方法接受一个闭包，该闭包解析原始数据字节以及应分配给附件的名称：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 获取邮件的附件</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@return</span> <span class="token class-name"><span class="token keyword">array</span></span>&lt;int, \Illuminate\Mail\Mailables\Attachment></span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">attachments</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">array</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token class-name static-context">Attachment</span><span class="token operator">::</span><span class="token function">fromData</span><span class="token punctuation">(</span><span class="token keyword">fn</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token property">pdf</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Report.pdf'</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">withMime</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'application/pdf'</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="内联附件" tabindex="-1"><a class="header-anchor" href="#内联附件"><span>内联附件</span></a></h3>
<p>在邮件中嵌入内联图片通常很麻烦；不过，Laravel 提供了一种将图像附加到邮件的便捷方法。可以使用邮件模板中 <code v-pre>$message</code> 变量的 <code v-pre>embed</code> 方法来嵌入内联图片。Laravel 自动使 <code v-pre>$message</code> 变量在全部邮件模板中可用，不需要担心手动传递它：</p>
<div class="language-blade line-numbers-mode" data-highlighter="prismjs" data-ext="blade" data-title="blade"><pre v-pre class="language-blade"><code><span class="line">&lt;body&gt;</span>
<span class="line">    Here is an image:</span>
<span class="line"></span>
<span class="line">    &lt;img src=&quot;{{ $message-&gt;embed($pathToImage) }}&quot;&gt;</span>
<span class="line">&lt;/body&gt;</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p><strong>警告</strong><br>
<code v-pre>$message</code> 在纯文本消息中不可用，因为文本消息不能使用内联附件</p>
</blockquote>
<h4 id="嵌入原始数据附件" tabindex="-1"><a class="header-anchor" href="#嵌入原始数据附件"><span>嵌入原始数据附件</span></a></h4>
<p>如果你希望嵌入一个已有的原始图像数据字符串到邮件模板中，可以在 <code v-pre>$message</code> 变量上调用 <code v-pre>embedData</code> 方法，当调用 <code v-pre>embedData</code> 方法时，你需要提供一个应分配给嵌入图像的文件名：</p>
<div class="language-blade line-numbers-mode" data-highlighter="prismjs" data-ext="blade" data-title="blade"><pre v-pre class="language-blade"><code><span class="line">&lt;body&gt;</span>
<span class="line">    以下是一张原始数据的图片:</span>
<span class="line"></span>
<span class="line">    &lt;img src=&quot;{{ $message-&gt;embedData($data, 'example-image.jpg') }}&quot;&gt;</span>
<span class="line">&lt;/body&gt;</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="可附加对象" tabindex="-1"><a class="header-anchor" href="#可附加对象"><span>可附加对象</span></a></h3>
<p>虽然通过简单的字符串路径将文件附加到消息通常就足够了，但在许多情况下，应用程序中的可附加实体由类表示。例如，如果你的应用程序正在将照片附加到消息中，那么你的应用程序也可能有一个表示该照片的 <code v-pre>Photo</code> 模型。在这种情况下，简单地将 <code v-pre>Photo</code> 模型传递给 <code v-pre>attach</code> 方法不是很方便吗？可附加对象允许你这么做。</p>
<p>要开始使用，在可附加到邮件的对象上实现 <code v-pre>Illuminate\Contracts\Mail\Attachable</code> 接口。此接口要求你的类定义一个 <code v-pre>toMailAttachment</code> 方法，该方法返回一个 <code v-pre>Illuminate\Mail\Attachment</code> 实例：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">namespace</span> <span class="token package">App<span class="token punctuation">\</span>Models</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Contracts<span class="token punctuation">\</span>Mail<span class="token punctuation">\</span>Attachable</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Database<span class="token punctuation">\</span>Eloquent<span class="token punctuation">\</span>Model</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Mail<span class="token punctuation">\</span>Attachment</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">Photo</span> <span class="token keyword">extends</span> <span class="token class-name">Model</span> <span class="token keyword">implements</span> <span class="token class-name">Attachable</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * 获取模型的可附加表示</span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">toMailAttachment</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token class-name return-type">Attachment</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token class-name static-context">Attachment</span><span class="token operator">::</span><span class="token function">fromPath</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/path/to/file'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>一旦定义了可附加对象，就可以在生成电子邮件时从 <code v-pre>attachments</code> 方法返回该对象的实例：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 获取邮件的附件</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@return</span> <span class="token class-name"><span class="token keyword">array</span></span>&lt;int, \Illuminate\Mail\Mailables\Attachment></span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">attachments</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">array</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token punctuation">[</span><span class="token variable">$this</span><span class="token operator">-></span><span class="token property">photo</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>当然，附件数据可能存储在远程文件存储服务上，例如 Amazon S3 。因此，Laravel还允许你从存储在应用程序的 <a href="https://learnku.com/docs/laravel/11.x/filesystem" target="_blank" rel="noopener noreferrer">文件系统磁盘</a>上的数据生成附件实例：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token comment">// 从默认磁盘上的文件创建附件</span></span>
<span class="line"><span class="token keyword">return</span> <span class="token class-name static-context">Attachment</span><span class="token operator">::</span><span class="token function">fromStorage</span><span class="token punctuation">(</span><span class="token variable">$this</span><span class="token operator">-></span><span class="token property">path</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 从指定磁盘上的文件创建附件</span></span>
<span class="line"><span class="token keyword">return</span> <span class="token class-name static-context">Attachment</span><span class="token operator">::</span><span class="token function">fromStorageDisk</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'backblaze'</span><span class="token punctuation">,</span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token property">path</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>另外，你可以通过内存中的数据创建附件实例。如此，需要提供一个闭包给 <code v-pre>fromData</code> 方法。闭包应返回附件的原始数据：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">return</span> <span class="token class-name static-context">Attachment</span><span class="token operator">::</span><span class="token function">fromData</span><span class="token punctuation">(</span><span class="token keyword">fn</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token property">content</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Photo Name'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>Laravel 还提供了其他方法，你可以使用这些方法来自定义附件。例如，您可以使用 <code v-pre>as</code> 和 <code v-pre>withMime</code> 方法来自定义文件的名称和 MIME 类型：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">return</span> <span class="token class-name static-context">Attachment</span><span class="token operator">::</span><span class="token function">fromPath</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/path/to/file'</span><span class="token punctuation">)</span></span>
<span class="line">        <span class="token operator">-></span><span class="token function">as</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Photo Name'</span><span class="token punctuation">)</span></span>
<span class="line">        <span class="token operator">-></span><span class="token function">withMime</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'image/jpeg'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="标头" tabindex="-1"><a class="header-anchor" href="#标头"><span>标头</span></a></h3>
<p>有时你可能需要在传出消息时添加附加的标头。例如，你可能需要设置一个自定义的 <code v-pre>Message-Id</code> 或其他任意文本标题。</p>
<p>为此，需要在你的 mailable 类上定义一个 <code v-pre>headers</code> 方法。 <code v-pre>headers</code> 方法应返回一个 <code v-pre>Illuminate\Mail\Mailables\Headers</code> 实例。此类接受 <code v-pre>messageId</code>，<code v-pre>references</code> 和 <code v-pre>text</code> 参数。当然，你可以只提供特定消息所需的参数：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Mail<span class="token punctuation">\</span>Mailables<span class="token punctuation">\</span>Headers</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 获取邮件标题.</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">headers</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token class-name return-type">Headers</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">Headers</span><span class="token punctuation">(</span></span>
<span class="line">        <span class="token argument-name">messageId</span><span class="token punctuation">:</span> <span class="token string single-quoted-string">'custom-message-id@example.com'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token argument-name">references</span><span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'previous-message@example.com'</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token argument-name">text</span><span class="token punctuation">:</span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'X-Custom-Header'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Custom Value'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="标签和元数据" tabindex="-1"><a class="header-anchor" href="#标签和元数据"><span>标签和元数据</span></a></h3>
<p>一些第三方电子邮件提供商支持邮件「标签」和「元数据」，如 Mailgun 和 Postmark ，可用于对对应用程序发送的电子邮件进行分组和跟踪。你可以通过 <code v-pre>Envelope</code> 来定义向电子邮件添加标签和元数据：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Mail<span class="token punctuation">\</span>Mailables<span class="token punctuation">\</span>Envelope</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 获取邮件信封.</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@return</span> <span class="token class-name"><span class="token punctuation">\</span>Illuminate<span class="token punctuation">\</span>Mail<span class="token punctuation">\</span>Mailables<span class="token punctuation">\</span>Envelope</span></span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">envelope</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token class-name return-type">Envelope</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">Envelope</span><span class="token punctuation">(</span></span>
<span class="line">        <span class="token argument-name">subject</span><span class="token punctuation">:</span> <span class="token string single-quoted-string">'Order Shipped'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token argument-name">tags</span><span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'shipment'</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token argument-name">metadata</span><span class="token punctuation">:</span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'order_id'</span> <span class="token operator">=></span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token property">order</span><span class="token operator">-></span><span class="token property">id</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果您的应用程序正在使用 Mailgun 驱动程序，您可以查阅 Mailgun 的文档以获取有关 <a href="https://documentation.mailgun.com/en/latest/user_manual.html#tagging-1" target="_blank" rel="noopener noreferrer">标签</a> 和 <a href="https://documentation.mailgun.com/en/latest/user_manual.html#attaching-data-to-messages" target="_blank" rel="noopener noreferrer">元数据</a> 的更多信息。同样，也可以查阅 Postmark 的文档以获取有关它们对 <a href="https://postmarkapp.com/blog/tags-support-for-smtp" target="_blank" rel="noopener noreferrer">标签</a> and <a href="https://postmarkapp.com/support/article/1125-custom-metadata-faq" target="_blank" rel="noopener noreferrer">元数据</a> 的支持的更多信息。</p>
<p>如果你的应用程序使用 Amazon SES 发送电子邮件，你应该使用 <code v-pre>metadata</code> 方法将 <a href="https://docs.aws.amazon.com/ses/latest/APIReference/API_MessageTag.html" target="_blank" rel="noopener noreferrer">SES “标签”</a> 附加到邮件上。</p>
<h3 id="自定义-symfony-消息" tabindex="-1"><a class="header-anchor" href="#自定义-symfony-消息"><span>自定义 Symfony 消息</span></a></h3>
<p>Laravel 的邮件功能是由 Symfony Mailer 提供支持的。Laravel 允许注册自定义回调，这些回调将在发送消息之前与 Symfony Message 实例一起调用。你可以在发送消息之前深入自定义消息。为此，在你的 <code v-pre>Envelope</code> 定义中定义一个 <code v-pre>using</code> 参数：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Mail<span class="token punctuation">\</span>Mailables<span class="token punctuation">\</span>Envelope</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Symfony<span class="token punctuation">\</span>Component<span class="token punctuation">\</span>Mime<span class="token punctuation">\</span>Email</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * Get the message envelope.</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">envelope</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token class-name return-type">Envelope</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">Envelope</span><span class="token punctuation">(</span></span>
<span class="line">        <span class="token argument-name">subject</span><span class="token punctuation">:</span> <span class="token string single-quoted-string">'Order Shipped'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token argument-name">using</span><span class="token punctuation">:</span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Email</span> <span class="token variable">$message</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">                <span class="token comment">// ...</span></span>
<span class="line">            <span class="token punctuation">}</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">]</span></span>
<span class="line">    <span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="markdown-邮件" tabindex="-1"><a class="header-anchor" href="#markdown-邮件"><span>Markdown 邮件</span></a></h2>
<p>Markdown 可发送邮件消息允许你在邮件中利用 <a href="https://learnku.com/docs/laravel/11.x/notifications#mail-notifications" target="_blank" rel="noopener noreferrer">邮件通知</a> 的预构建模板和组件。由于消息是用 Markdown 编写的，Laravel 能够为消息渲染美观、响应式的 HTML 模板，同时自动生成纯文本版本。</p>
<h3 id="生成-markdown-邮件" tabindex="-1"><a class="header-anchor" href="#生成-markdown-邮件"><span>生成 Markdown 邮件</span></a></h3>
<p>要生成带有相应 Markdown 模板的可发送邮件，你可以使用 <code v-pre>make:mail Artisan</code> 命令的 <code v-pre>--markdown</code> 选项：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">php artisan make:mail OrderShipped <span class="token parameter variable">--markdown</span><span class="token operator">=</span>mail.orders.shipped</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>然后，在其 <code v-pre>content</code> 方法中配置可发送邮件的 <code v-pre>Content</code> 定义时，使用 <code v-pre>markdown</code> 参数代替 <code v-pre>view</code> 参数：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Mail<span class="token punctuation">\</span>Mailables<span class="token punctuation">\</span>Content</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * Get the message content definition.</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">content</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token class-name return-type">Content</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">Content</span><span class="token punctuation">(</span></span>
<span class="line">        <span class="token argument-name">markdown</span><span class="token punctuation">:</span> <span class="token string single-quoted-string">'mail.orders.shipped'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token argument-name">with</span><span class="token punctuation">:</span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'url'</span> <span class="token operator">=></span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token property">orderUrl</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="编写-markdown-消息" tabindex="-1"><a class="header-anchor" href="#编写-markdown-消息"><span>编写 Markdown 消息</span></a></h3>
<p>Markdown mailable 类整合了 Markdown 语法和 Blade 组件，让你能够非常方便的使用 Laravel 预置的 UI 组件来构建邮件消息：</p>
<div class="language-blade line-numbers-mode" data-highlighter="prismjs" data-ext="blade" data-title="blade"><pre v-pre class="language-blade"><code><span class="line">&lt;x-mail::message&gt;</span>
<span class="line"># 订单发货</span>
<span class="line"></span>
<span class="line">你的订单已发货！</span>
<span class="line"></span>
<span class="line">&lt;x-mail::button :url=&quot;$url&quot;&gt;</span>
<span class="line">查看订单</span>
<span class="line">&lt;/x-mail::button&gt;</span>
<span class="line"></span>
<span class="line">谢谢,&lt;br&gt;</span>
<span class="line">{{ config('app.name') }}</span>
<span class="line">&lt;/x-mail::message&gt;</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p><strong>提示</strong><br>
在编写 Markdown 邮件的时候，请勿使用额外的缩进。Markdown 解析器会把缩进渲染成代码块。</p>
</blockquote>
<h4 id="按钮组件" tabindex="-1"><a class="header-anchor" href="#按钮组件"><span>按钮组件</span></a></h4>
<p>按钮组件用于渲染居中的按钮链接。该组件接收两个参数，一个是 <code v-pre>url</code> 一个是可选的 <code v-pre>color</code>。 支持的颜色包括 <code v-pre>primary</code>、 <code v-pre>success</code> 和 <code v-pre>error</code>。你可以在邮件中添加任意数量的按钮组件：</p>
<div class="language-blade line-numbers-mode" data-highlighter="prismjs" data-ext="blade" data-title="blade"><pre v-pre class="language-blade"><code><span class="line">&lt;x-mail::button :url=&quot;$url&quot; color=&quot;success&quot;&gt;</span>
<span class="line">查看订单</span>
<span class="line">&lt;/x-mail::button&gt;</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="面板组件" tabindex="-1"><a class="header-anchor" href="#面板组件"><span>面板组件</span></a></h4>
<p>面板组件在面板内渲染指定的文本块，面板与其他消息的背景色略有不同。它允许你绘制一个警示文本块：</p>
<div class="language-blade line-numbers-mode" data-highlighter="prismjs" data-ext="blade" data-title="blade"><pre v-pre class="language-blade"><code><span class="line">&lt;x-mail::panel&gt;</span>
<span class="line">这是面板内容</span>
<span class="line">&lt;/x-mail::panel&gt;</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="表格组件" tabindex="-1"><a class="header-anchor" href="#表格组件"><span>表格组件</span></a></h4>
<p>表格组件允许你将 Markdown 表格转换成 HTML 表格。该组件接受 Markdown 表格作为其内容。列对齐支持默认的 Markdown 表格对齐语法：</p>
<div class="language-blade line-numbers-mode" data-highlighter="prismjs" data-ext="blade" data-title="blade"><pre v-pre class="language-blade"><code><span class="line">&lt;x-mail::table&gt;</span>
<span class="line">| Laravel       | Table         | Example  |</span>
<span class="line">| ------------- |:-------------:| --------:|</span>
<span class="line">| Col 2 is      | Centered      | $10      |</span>
<span class="line">| Col 3 is      | Right-Aligned | $20      |</span>
<span class="line">&lt;/x-mail::table&gt;</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="自定义组件" tabindex="-1"><a class="header-anchor" href="#自定义组件"><span>自定义组件</span></a></h3>
<p>你可以将所有 Markdown 邮件组件导出到自己的应用，用作自定义组件的模板。若要导出组件，需要在使用 <code v-pre>vendor:publish</code> 命令时加上 <code v-pre>--tag=laravel-mail</code>:</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">php artisan vendor:publish <span class="token parameter variable">--tag</span><span class="token operator">=</span>laravel-mail</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>此命令会将 Markdown 邮件组件导出到 <code v-pre>resources/views/vendor/mail</code> 目录。该 <code v-pre>mail</code> 目录包含 <code v-pre>html</code> 和 <code v-pre>text</code> 子目录，分别包含各自对应的可用组件描述。你可以按照自己的意愿自定义这些组件。</p>
<h4 id="自定义-css" tabindex="-1"><a class="header-anchor" href="#自定义-css"><span>自定义 CSS</span></a></h4>
<p>组件导出后，<code v-pre>resources/views/vendor/mail/html/themes</code> 目录下有一个 <code v-pre>default.css</code> 文件。可以在此文件中自定义 CSS，这些样式将自动内联到 Markdown 邮件消息的 HTML 表示中。</p>
<p>如果想为 Laravel 的 Markdown 组件构建一个全新的主题，你可以在 <code v-pre>html/themes</code> 目录中新建一个 CSS 文件。命名并保存 CSS 文件后，并更新应用程序 <code v-pre>config/mail.php</code> 配置文件的 <code v-pre>theme</code> 选项以匹配新主题的名称。</p>
<p>要想自定义单个邮件主题，可以将 mailable 类的 <code v-pre>$theme</code> 属性设置为发送 mailable 时应使用的主题名称。</p>
<h2 id="发送邮件-1" tabindex="-1"><a class="header-anchor" href="#发送邮件-1"><span>发送邮件</span></a></h2>
<p>若要发送邮件，使用 <code v-pre>Mail</code> <a href="https://learnku.com/docs/laravel/11.x/facades" target="_blank" rel="noopener noreferrer">facade</a> 的方法。该 <code v-pre>to</code> 方法接受 邮件地址、用户实例或用户集合。如果传递一个对象或者对象集合，mailer 在设置收件人时将自动使用它们的 <code v-pre>email</code> 和 <code v-pre>name</code> 属性，因此请确保对象的这些属性可用。一旦指定了收件人，就可以将 mailable 类实例传递给 <code v-pre>send</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">namespace</span> <span class="token package">App<span class="token punctuation">\</span>Http<span class="token punctuation">\</span>Controllers</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Http<span class="token punctuation">\</span>Controllers<span class="token punctuation">\</span>Controller</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Mail<span class="token punctuation">\</span>OrderShipped</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Models<span class="token punctuation">\</span>Order</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Http<span class="token punctuation">\</span>RedirectResponse</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Http<span class="token punctuation">\</span>Request</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Mail</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">OrderShipmentController</span> <span class="token keyword">extends</span> <span class="token class-name">Controller</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * 发送给定的订单信息。</span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">store</span><span class="token punctuation">(</span><span class="token class-name type-declaration">Request</span> <span class="token variable">$request</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token class-name return-type">RedirectResponse</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token variable">$order</span> <span class="token operator">=</span> <span class="token class-name static-context">Order</span><span class="token operator">::</span><span class="token function">findOrFail</span><span class="token punctuation">(</span><span class="token variable">$request</span><span class="token operator">-></span><span class="token property">order_id</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">        <span class="token comment">// 发货订单。。。</span></span>
<span class="line"></span>
<span class="line">        <span class="token class-name static-context">Mail</span><span class="token operator">::</span><span class="token function">to</span><span class="token punctuation">(</span><span class="token variable">$request</span><span class="token operator">-></span><span class="token function">user</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">send</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">OrderShipped</span><span class="token punctuation">(</span><span class="token variable">$order</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">        <span class="token keyword">return</span> <span class="token function">redirect</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/orders'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>在发送消息时不止可以指定「收件人」。还可以通过链式调用「to」、「cc」、「bcc」一次性指定抄送和密送收件人：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Mail</span><span class="token operator">::</span><span class="token function">to</span><span class="token punctuation">(</span><span class="token variable">$request</span><span class="token operator">-></span><span class="token function">user</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">cc</span><span class="token punctuation">(</span><span class="token variable">$moreUsers</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">bcc</span><span class="token punctuation">(</span><span class="token variable">$evenMoreUsers</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">send</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">OrderShipped</span><span class="token punctuation">(</span><span class="token variable">$order</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="遍历收件人列表" tabindex="-1"><a class="header-anchor" href="#遍历收件人列表"><span>遍历收件人列表</span></a></h4>
<p>有时，你可能需要通过迭代收件人/电子邮件地址数组的方式向收件人列表发送邮件。然而，由于 <code v-pre>to</code> 方法将电子邮件地址附加到邮件的收件人列表中，循环中的每次迭代都会再次向每个先前的收件人发送另一封电子邮件。因此，你应始终为每个收件人重新创建 mailable 实例：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">foreach</span> <span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'taylor@example.com'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'dries@example.com'</span><span class="token punctuation">]</span> <span class="token keyword">as</span> <span class="token variable">$recipient</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token class-name static-context">Mail</span><span class="token operator">::</span><span class="token function">to</span><span class="token punctuation">(</span><span class="token variable">$recipient</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">send</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">OrderShipped</span><span class="token punctuation">(</span><span class="token variable">$order</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="通过特定的-mailer-发送邮件" tabindex="-1"><a class="header-anchor" href="#通过特定的-mailer-发送邮件"><span>通过特定的 Mailer 发送邮件</span></a></h4>
<p>默认情况下，Laravel 将使用在应用程序的 <code v-pre>mail</code> 配置文件中配置为 <code v-pre>default</code> 的邮件程序发送电子邮件。但是，你可以使用 <code v-pre>mailer</code> 方法通过特定的邮件配置发送消息：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Mail</span><span class="token operator">::</span><span class="token function">mailer</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'postmark'</span><span class="token punctuation">)</span></span>
<span class="line">        <span class="token operator">-></span><span class="token function">to</span><span class="token punctuation">(</span><span class="token variable">$request</span><span class="token operator">-></span><span class="token function">user</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span></span>
<span class="line">        <span class="token operator">-></span><span class="token function">send</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">OrderShipped</span><span class="token punctuation">(</span><span class="token variable">$order</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="邮件队列" tabindex="-1"><a class="header-anchor" href="#邮件队列"><span>邮件队列</span></a></h3>
<h4 id="将邮件消息加入队列" tabindex="-1"><a class="header-anchor" href="#将邮件消息加入队列"><span>将邮件消息加入队列</span></a></h4>
<p>由于发送电子邮件消息可能会对应用程序的响应时间产生负面影响，许多开发人员选择将电子邮件消息加入队列以进行后台发送。Laravel 使用其内置的<a href="https://learnku.com/docs/laravel/11.x/queuesmd" target="_blank" rel="noopener noreferrer">统一队列 API</a>使这变得容易。要排将邮件消息加入队列，你可以在指定收件人后使用 <code v-pre>Mail</code> facade 上的 <code v-pre>queue</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Mail</span><span class="token operator">::</span><span class="token function">to</span><span class="token punctuation">(</span><span class="token variable">$request</span><span class="token operator">-></span><span class="token function">user</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">cc</span><span class="token punctuation">(</span><span class="token variable">$moreUsers</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">bcc</span><span class="token punctuation">(</span><span class="token variable">$evenMoreUsers</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">queue</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">OrderShipped</span><span class="token punctuation">(</span><span class="token variable">$order</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>此方法自动将作业推送到队列中以便消息在后台发送。使用此特性之前，需要 <a href="https://learnku.com/docs/laravel/11.x/queuesmd" target="_blank" rel="noopener noreferrer">配置队列</a> 。</p>
<h4 id="延迟消息队列" tabindex="-1"><a class="header-anchor" href="#延迟消息队列"><span>延迟消息队列</span></a></h4>
<p>如果你希望延迟发送队列化的邮件消息，可以使用 <code v-pre>later</code> 方法。<code v-pre>later</code> 方法的第一个参数接受一个 <code v-pre>DateTime</code> 实例，指示何时应发送消息：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Mail</span><span class="token operator">::</span><span class="token function">to</span><span class="token punctuation">(</span><span class="token variable">$request</span><span class="token operator">-></span><span class="token function">user</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">cc</span><span class="token punctuation">(</span><span class="token variable">$moreUsers</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">bcc</span><span class="token punctuation">(</span><span class="token variable">$evenMoreUsers</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">later</span><span class="token punctuation">(</span><span class="token function">now</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">addMinutes</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token keyword">new</span> <span class="token class-name">OrderShipped</span><span class="token punctuation">(</span><span class="token variable">$order</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="推送到指定队列" tabindex="-1"><a class="header-anchor" href="#推送到指定队列"><span>推送到指定队列</span></a></h4>
<p>由于使用 <code v-pre>make:mail</code> 命令生成的所有 mailable 类都使用了 <code v-pre>Illuminate\Bus\Queueable</code> trait，因此你可以在任何 mailable 类实例上调用 <code v-pre>onQueue</code> 和 <code v-pre>onConnection</code> 方法来指定消息的连接和队列名称：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$message</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">OrderShipped</span><span class="token punctuation">(</span><span class="token variable">$order</span><span class="token punctuation">)</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">onConnection</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'sqs'</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">onQueue</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'emails'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Mail</span><span class="token operator">::</span><span class="token function">to</span><span class="token punctuation">(</span><span class="token variable">$request</span><span class="token operator">-></span><span class="token function">user</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">cc</span><span class="token punctuation">(</span><span class="token variable">$moreUsers</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">bcc</span><span class="token punctuation">(</span><span class="token variable">$evenMoreUsers</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">queue</span><span class="token punctuation">(</span><span class="token variable">$message</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="默认队列" tabindex="-1"><a class="header-anchor" href="#默认队列"><span>默认队列</span></a></h4>
<p>如果你希望你的邮件类始终使用队列，你可以给邮件类实现 <code v-pre>ShouldQueue</code> 契约，现在即使你调用了 <code v-pre>send</code> 方法，邮件依旧使用队列的方式发送：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Contracts<span class="token punctuation">\</span>Queue<span class="token punctuation">\</span>ShouldQueue</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">OrderShipped</span> <span class="token keyword">extends</span> <span class="token class-name">Mailable</span> <span class="token keyword">implements</span> <span class="token class-name">ShouldQueue</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token comment">// ...</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="邮件队列和数据库事务" tabindex="-1"><a class="header-anchor" href="#邮件队列和数据库事务"><span>邮件队列和数据库事务</span></a></h4>
<p>当在数据库事务中分发邮件队列时，队列可能在数据库事务提交之前处理邮件。 发生这种情况时，在数据库事务期间对模型或数据库记录所做的任何更新可能都不会反映在数据库中。另外，在事务中创建的任何模型或数据库记录都可能不存在于数据库中。如果你的邮件基于以上这些模型数据，则在处理邮件发送时，可能会发生意外错误。</p>
<p>如果队列连接的 <code v-pre>after_commit</code> 配置选项设置为 <code v-pre>false</code>，那么仍然可以通过在邮件类上定义 <code v-pre>after_commit</code> 属性来设置提交所有打开的数据库事务之后再调度特定的邮件队列：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Mail</span><span class="token operator">::</span><span class="token function">to</span><span class="token punctuation">(</span><span class="token variable">$request</span><span class="token operator">-></span><span class="token function">user</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">send</span><span class="token punctuation">(</span></span>
<span class="line">    <span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">OrderShipped</span><span class="token punctuation">(</span><span class="token variable">$order</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">afterCommit</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line"><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>或者，你可以在邮件类的构造函数中调用 <code v-pre>afterCommit</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">namespace</span> <span class="token package">App<span class="token punctuation">\</span>Mail</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Bus<span class="token punctuation">\</span>Queueable</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Contracts<span class="token punctuation">\</span>Queue<span class="token punctuation">\</span>ShouldQueue</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Mail<span class="token punctuation">\</span>Mailable</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Queue<span class="token punctuation">\</span>SerializesModels</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">OrderShipped</span> <span class="token keyword">extends</span> <span class="token class-name">Mailable</span> <span class="token keyword">implements</span> <span class="token class-name">ShouldQueue</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">use</span> <span class="token package">Queueable</span><span class="token punctuation">,</span> SerializesModels<span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * 创建一个新的消息实例。</span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">__construct</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">afterCommit</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p><strong>技巧</strong><br>
要了解如何解决这些问题的更多信息，请查看<a href="https://learnku.com/docs/laravel/11.x/queuesmd#jobs-and-database-transactions" target="_blank" rel="noopener noreferrer">队列任务和数据库事务</a> 。</p>
</blockquote>
<h2 id="渲染邮件" tabindex="-1"><a class="header-anchor" href="#渲染邮件"><span>渲染邮件</span></a></h2>
<p>有时你可能希望捕获邮件的 HTML 内容而不发送它。为此，可以调用邮件类的 <code v-pre>render</code> 方法。此方法将以字符串形式返回邮件类的渲染内容:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Mail<span class="token punctuation">\</span>InvoicePaid</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Models<span class="token punctuation">\</span>Invoice</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$invoice</span> <span class="token operator">=</span> <span class="token class-name static-context">Invoice</span><span class="token operator">::</span><span class="token function">find</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">return</span> <span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">InvoicePaid</span><span class="token punctuation">(</span><span class="token variable">$invoice</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">render</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="在浏览器中预览邮件" tabindex="-1"><a class="header-anchor" href="#在浏览器中预览邮件"><span>在浏览器中预览邮件</span></a></h3>
<p>设计邮件模板时，可以方便地在浏览器中预览邮件，就像典型的 Blade 模板一样。因此， Laravel 允许你直接从路由闭包或控制器返回任何邮件类。当邮件返回时，它将渲染并显示在浏览器中，让你能够在不需要将其发送到实际电子邮件地址的情况下快速预览它的设计：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Route</span><span class="token operator">::</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/mailable'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$invoice</span> <span class="token operator">=</span> <span class="token class-name class-name-fully-qualified static-context">App<span class="token punctuation">\</span>Models<span class="token punctuation">\</span>Invoice</span><span class="token operator">::</span><span class="token function">find</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name class-name-fully-qualified">App<span class="token punctuation">\</span>Mail<span class="token punctuation">\</span>InvoicePaid</span><span class="token punctuation">(</span><span class="token variable">$invoice</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="本地化邮件" tabindex="-1"><a class="header-anchor" href="#本地化邮件"><span>本地化邮件</span></a></h2>
<p>Laravel 允许你在与请求的当前语言环境不同的语言环境中发送邮件，如果邮件被队列化，它甚至会记住这个语言环境。</p>
<p>为了实现这一点，<code v-pre>Mail</code> facade 提供了一个 <code v-pre>locale</code> 方法来设置所需的语言。当邮件的模板被评估时，应用程序将会改变到这个语言环境，然后在评估完成后恢复到之前的语言环境：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Mail</span><span class="token operator">::</span><span class="token function">to</span><span class="token punctuation">(</span><span class="token variable">$request</span><span class="token operator">-></span><span class="token function">user</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">locale</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'es'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">send</span><span class="token punctuation">(</span></span>
<span class="line">    <span class="token keyword">new</span> <span class="token class-name">OrderShipped</span><span class="token punctuation">(</span><span class="token variable">$order</span><span class="token punctuation">)</span></span>
<span class="line"><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="用户首选语言环境" tabindex="-1"><a class="header-anchor" href="#用户首选语言环境"><span>用户首选语言环境</span></a></h3>
<p>有时，应用程序会存储每个用户的首选语言环境。通过在一个或多个模型上实现 <code v-pre>HasLocalePreference</code> 契约，你可以指示 Laravel 使用这个存储的语言环境来发送邮件：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Contracts<span class="token punctuation">\</span>Translation<span class="token punctuation">\</span>HasLocalePreference</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">User</span> <span class="token keyword">extends</span> <span class="token class-name">Model</span> <span class="token keyword">implements</span> <span class="token class-name">HasLocalePreference</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * 获取用户的区域设置。</span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">preferredLocale</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">string</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token property">locale</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>一旦你实现了接口，Laravel 将在向模型发送邮件和通知时自动使用首选语言环境。 因此，使用该接口时无需调用 <code v-pre>locale</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Mail</span><span class="token operator">::</span><span class="token function">to</span><span class="token punctuation">(</span><span class="token variable">$request</span><span class="token operator">-></span><span class="token function">user</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">send</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">OrderShipped</span><span class="token punctuation">(</span><span class="token variable">$order</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h2 id="测试邮件" tabindex="-1"><a class="header-anchor" href="#测试邮件"><span>测试邮件</span></a></h2>
<h3 id="测试邮件内容" tabindex="-1"><a class="header-anchor" href="#测试邮件内容"><span>测试邮件内容</span></a></h3>
<p>Laravel 提供了多种方法来检查你的邮件的结构。此外，Laravel 提供了几个方便的方法来测试你的邮件是否包含你期望的内容。这些方法包括：<code v-pre>assertSeeInHtml</code>、<code v-pre>assertDontSeeInHtml</code>、<code v-pre>assertSeeInOrderInHtml</code>、<code v-pre>assertSeeInText</code>、<code v-pre>assertDontSeeInText</code>、<code v-pre>assertSeeInOrderInText</code>、<code v-pre>assertHasAttachment</code>、<code v-pre>assertHasAttachedData</code>、<code v-pre>assertHasAttachmentFromStorage</code> 以及 <code v-pre>assertHasAttachmentFromStorageDisk</code>。</p>
<p>正如你所期望的，「HTML」断言会断言你的邮件的 HTML 版本是否包含给定的字符串，而「text」断言断言邮件的纯文本版本是否包含给定的字符串：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token comment">//译者注：Pest 示例</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Mail<span class="token punctuation">\</span>InvoicePaid</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Models<span class="token punctuation">\</span>User</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token function">test</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'mailable content'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$user</span> <span class="token operator">=</span> <span class="token class-name static-context">User</span><span class="token operator">::</span><span class="token function">factory</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">create</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$mailable</span> <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">InvoicePaid</span><span class="token punctuation">(</span><span class="token variable">$user</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$mailable</span><span class="token operator">-></span><span class="token function">assertFrom</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'jeffrey@example.com'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token variable">$mailable</span><span class="token operator">-></span><span class="token function">assertTo</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'taylor@example.com'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token variable">$mailable</span><span class="token operator">-></span><span class="token function">assertHasCc</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'abigail@example.com'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token variable">$mailable</span><span class="token operator">-></span><span class="token function">assertHasBcc</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'victoria@example.com'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token variable">$mailable</span><span class="token operator">-></span><span class="token function">assertHasReplyTo</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'tyler@example.com'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token variable">$mailable</span><span class="token operator">-></span><span class="token function">assertHasSubject</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Invoice Paid'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token variable">$mailable</span><span class="token operator">-></span><span class="token function">assertHasTag</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'example-tag'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token variable">$mailable</span><span class="token operator">-></span><span class="token function">assertHasMetadata</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'key'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'value'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$mailable</span><span class="token operator">-></span><span class="token function">assertSeeInHtml</span><span class="token punctuation">(</span><span class="token variable">$user</span><span class="token operator">-></span><span class="token property">email</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token variable">$mailable</span><span class="token operator">-></span><span class="token function">assertSeeInHtml</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Invoice Paid'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token variable">$mailable</span><span class="token operator">-></span><span class="token function">assertSeeInOrderInHtml</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'Invoice Paid'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Thanks'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$mailable</span><span class="token operator">-></span><span class="token function">assertSeeInText</span><span class="token punctuation">(</span><span class="token variable">$user</span><span class="token operator">-></span><span class="token property">email</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token variable">$mailable</span><span class="token operator">-></span><span class="token function">assertSeeInOrderInText</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'Invoice Paid'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Thanks'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$mailable</span><span class="token operator">-></span><span class="token function">assertHasAttachment</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/path/to/file'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token variable">$mailable</span><span class="token operator">-></span><span class="token function">assertHasAttachment</span><span class="token punctuation">(</span><span class="token class-name static-context">Attachment</span><span class="token operator">::</span><span class="token function">fromPath</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/path/to/file'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token variable">$mailable</span><span class="token operator">-></span><span class="token function">assertHasAttachedData</span><span class="token punctuation">(</span><span class="token variable">$pdfData</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'name.pdf'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'mime'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'application/pdf'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token variable">$mailable</span><span class="token operator">-></span><span class="token function">assertHasAttachmentFromStorage</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/path/to/file'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'name.pdf'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'mime'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'application/pdf'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token variable">$mailable</span><span class="token operator">-></span><span class="token function">assertHasAttachmentFromStorageDisk</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'s3'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'/path/to/file'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'name.pdf'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'mime'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'application/pdf'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token comment">//译者注：PHPUnit 示例</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Mail<span class="token punctuation">\</span>InvoicePaid</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Models<span class="token punctuation">\</span>User</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">test_mailable_content</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">void</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$user</span> <span class="token operator">=</span> <span class="token class-name static-context">User</span><span class="token operator">::</span><span class="token function">factory</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">create</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$mailable</span> <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">InvoicePaid</span><span class="token punctuation">(</span><span class="token variable">$user</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$mailable</span><span class="token operator">-></span><span class="token function">assertFrom</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'jeffrey@example.com'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token variable">$mailable</span><span class="token operator">-></span><span class="token function">assertTo</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'taylor@example.com'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token variable">$mailable</span><span class="token operator">-></span><span class="token function">assertHasCc</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'abigail@example.com'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token variable">$mailable</span><span class="token operator">-></span><span class="token function">assertHasBcc</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'victoria@example.com'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token variable">$mailable</span><span class="token operator">-></span><span class="token function">assertHasReplyTo</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'tyler@example.com'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token variable">$mailable</span><span class="token operator">-></span><span class="token function">assertHasSubject</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Invoice Paid'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token variable">$mailable</span><span class="token operator">-></span><span class="token function">assertHasTag</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'example-tag'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token variable">$mailable</span><span class="token operator">-></span><span class="token function">assertHasMetadata</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'key'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'value'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$mailable</span><span class="token operator">-></span><span class="token function">assertSeeInHtml</span><span class="token punctuation">(</span><span class="token variable">$user</span><span class="token operator">-></span><span class="token property">email</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token variable">$mailable</span><span class="token operator">-></span><span class="token function">assertSeeInHtml</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Invoice Paid'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token variable">$mailable</span><span class="token operator">-></span><span class="token function">assertSeeInOrderInHtml</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'Invoice Paid'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Thanks'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$mailable</span><span class="token operator">-></span><span class="token function">assertSeeInText</span><span class="token punctuation">(</span><span class="token variable">$user</span><span class="token operator">-></span><span class="token property">email</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token variable">$mailable</span><span class="token operator">-></span><span class="token function">assertSeeInOrderInText</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'Invoice Paid'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Thanks'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$mailable</span><span class="token operator">-></span><span class="token function">assertHasAttachment</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/path/to/file'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token variable">$mailable</span><span class="token operator">-></span><span class="token function">assertHasAttachment</span><span class="token punctuation">(</span><span class="token class-name static-context">Attachment</span><span class="token operator">::</span><span class="token function">fromPath</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/path/to/file'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token variable">$mailable</span><span class="token operator">-></span><span class="token function">assertHasAttachedData</span><span class="token punctuation">(</span><span class="token variable">$pdfData</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'name.pdf'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'mime'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'application/pdf'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token variable">$mailable</span><span class="token operator">-></span><span class="token function">assertHasAttachmentFromStorage</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/path/to/file'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'name.pdf'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'mime'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'application/pdf'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token variable">$mailable</span><span class="token operator">-></span><span class="token function">assertHasAttachmentFromStorageDisk</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'s3'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'/path/to/file'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'name.pdf'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'mime'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'application/pdf'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="测试邮件发送" tabindex="-1"><a class="header-anchor" href="#测试邮件发送"><span>测试邮件发送</span></a></h3>
<p>我们建议将邮件内容和判断指定的邮件「发送」给特定用户的测试分开进行测试。通常来讲，邮件的内容与你正在测试的代码无关，只要能简单地判断 Laravel 能够发送指定的邮件就足够了。</p>
<p>你可以使用 <code v-pre>Mail</code> facade 的 <code v-pre>fake</code> 方法来阻止邮件的发送。调用了 <code v-pre>Mail</code> facade 的 <code v-pre>fake</code> 方法后，你可以判断邮件是否已被发送给指定的用户，甚至可以检查邮件收到的数据：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"><span class="token comment">//译者注：Pest 示例</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Mail<span class="token punctuation">\</span>OrderShipped</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Mail</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token function">test</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'orders can be shipped'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token class-name static-context">Mail</span><span class="token operator">::</span><span class="token function">fake</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// 执行订单发货...</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// 断言没有邮件被发送...</span></span>
<span class="line">    <span class="token class-name static-context">Mail</span><span class="token operator">::</span><span class="token function">assertNothingSent</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// 断言已发送邮件...</span></span>
<span class="line">    <span class="token class-name static-context">Mail</span><span class="token operator">::</span><span class="token function">assertSent</span><span class="token punctuation">(</span><span class="token class-name static-context">OrderShipped</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// 断言已发送两次的邮件...</span></span>
<span class="line">    <span class="token class-name static-context">Mail</span><span class="token operator">::</span><span class="token function">assertSent</span><span class="token punctuation">(</span><span class="token class-name static-context">OrderShipped</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// 断言邮件未发送...</span></span>
<span class="line">    <span class="token class-name static-context">Mail</span><span class="token operator">::</span><span class="token function">assertNotSent</span><span class="token punctuation">(</span><span class="token class-name static-context">AnotherMailable</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// 断言总共发送了3封邮件...</span></span>
<span class="line">    <span class="token class-name static-context">Mail</span><span class="token operator">::</span><span class="token function">assertSentCount</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"><span class="token comment">//译者注：PHPUnit 示例</span></span>
<span class="line"><span class="token keyword">namespace</span> <span class="token package">Tests<span class="token punctuation">\</span>Feature</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Mail<span class="token punctuation">\</span>OrderShipped</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Mail</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Tests<span class="token punctuation">\</span>TestCase</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">ExampleTest</span> <span class="token keyword">extends</span> <span class="token class-name">TestCase</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">test_orders_can_be_shipped</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">void</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token class-name static-context">Mail</span><span class="token operator">::</span><span class="token function">fake</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">        <span class="token comment">// 执行订单发货...</span></span>
<span class="line"></span>
<span class="line">        <span class="token comment">// 断言没有发送邮件...</span></span>
<span class="line">        <span class="token class-name static-context">Mail</span><span class="token operator">::</span><span class="token function">assertNothingSent</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">        <span class="token comment">// 断言已发送邮件...</span></span>
<span class="line">        <span class="token class-name static-context">Mail</span><span class="token operator">::</span><span class="token function">assertSent</span><span class="token punctuation">(</span><span class="token class-name static-context">OrderShipped</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">        <span class="token comment">// 断言已发送两次的邮件...</span></span>
<span class="line">        <span class="token class-name static-context">Mail</span><span class="token operator">::</span><span class="token function">assertSent</span><span class="token punctuation">(</span><span class="token class-name static-context">OrderShipped</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">        <span class="token comment">// 断言邮件未发送...</span></span>
<span class="line">        <span class="token class-name static-context">Mail</span><span class="token operator">::</span><span class="token function">assertNotSent</span><span class="token punctuation">(</span><span class="token class-name static-context">AnotherMailable</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">        <span class="token comment">// 断言总共发送了3封邮件...</span></span>
<span class="line">        <span class="token class-name static-context">Mail</span><span class="token operator">::</span><span class="token function">assertSentCount</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果你在后台排队等待邮件的发送，则应该使用 <code v-pre>assertQueued</code> 方法而不是 <code v-pre>assertSent</code> 方法。</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Mail</span><span class="token operator">::</span><span class="token function">assertQueued</span><span class="token punctuation">(</span><span class="token class-name static-context">OrderShipped</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token class-name static-context">Mail</span><span class="token operator">::</span><span class="token function">assertNotQueued</span><span class="token punctuation">(</span><span class="token class-name static-context">OrderShipped</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token class-name static-context">Mail</span><span class="token operator">::</span><span class="token function">assertNothingQueued</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token class-name static-context">Mail</span><span class="token operator">::</span><span class="token function">assertQueuedCount</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>你可以向 <code v-pre>assertSent</code>, <code v-pre>assertNotSent</code>, <code v-pre>assertQueued</code>, 或 <code v-pre>assertNotQueued</code> 方法传递一个闭包，以来判断发送的邮件是否通过给定的 「真值检验」。如果至少有一个通过给定真值测试的邮件被发送，那么断言将是成功的：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Mail</span><span class="token operator">::</span><span class="token function">assertSent</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">OrderShipped</span> <span class="token variable">$mail</span><span class="token punctuation">)</span> <span class="token keyword">use</span> <span class="token punctuation">(</span><span class="token variable">$order</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token variable">$mail</span><span class="token operator">-></span><span class="token property">order</span><span class="token operator">-></span><span class="token property">id</span> <span class="token operator">===</span> <span class="token variable">$order</span><span class="token operator">-></span><span class="token property">id</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>当调用 <code v-pre>Mail</code> facade的断言方法时，所提供的闭包接受的邮件实例提供了检查邮件的有用方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Mail</span><span class="token operator">::</span><span class="token function">assertSent</span><span class="token punctuation">(</span><span class="token class-name static-context">OrderShipped</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">OrderShipped</span> <span class="token variable">$mail</span><span class="token punctuation">)</span> <span class="token keyword">use</span> <span class="token punctuation">(</span><span class="token variable">$user</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token variable">$mail</span><span class="token operator">-></span><span class="token function">hasTo</span><span class="token punctuation">(</span><span class="token variable">$user</span><span class="token operator">-></span><span class="token property">email</span><span class="token punctuation">)</span> <span class="token operator">&amp;&amp;</span></span>
<span class="line">           <span class="token variable">$mail</span><span class="token operator">-></span><span class="token function">hasCc</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'...'</span><span class="token punctuation">)</span> <span class="token operator">&amp;&amp;</span></span>
<span class="line">           <span class="token variable">$mail</span><span class="token operator">-></span><span class="token function">hasBcc</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'...'</span><span class="token punctuation">)</span> <span class="token operator">&amp;&amp;</span></span>
<span class="line">           <span class="token variable">$mail</span><span class="token operator">-></span><span class="token function">hasReplyTo</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'...'</span><span class="token punctuation">)</span> <span class="token operator">&amp;&amp;</span></span>
<span class="line">           <span class="token variable">$mail</span><span class="token operator">-></span><span class="token function">hasFrom</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'...'</span><span class="token punctuation">)</span> <span class="token operator">&amp;&amp;</span></span>
<span class="line">           <span class="token variable">$mail</span><span class="token operator">-></span><span class="token function">hasSubject</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'...'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>邮件实例还包括几个有用的方法来检查邮件上的附件:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Mail<span class="token punctuation">\</span>Mailables<span class="token punctuation">\</span>Attachment</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Mail</span><span class="token operator">::</span><span class="token function">assertSent</span><span class="token punctuation">(</span><span class="token class-name static-context">OrderShipped</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">OrderShipped</span> <span class="token variable">$mail</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token variable">$mail</span><span class="token operator">-></span><span class="token function">hasAttachment</span><span class="token punctuation">(</span></span>
<span class="line">        <span class="token class-name static-context">Attachment</span><span class="token operator">::</span><span class="token function">fromPath</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/path/to/file'</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">as</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name.pdf'</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">withMime</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'application/pdf'</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Mail</span><span class="token operator">::</span><span class="token function">assertSent</span><span class="token punctuation">(</span><span class="token class-name static-context">OrderShipped</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">OrderShipped</span> <span class="token variable">$mail</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token variable">$mail</span><span class="token operator">-></span><span class="token function">hasAttachment</span><span class="token punctuation">(</span></span>
<span class="line">        <span class="token class-name static-context">Attachment</span><span class="token operator">::</span><span class="token function">fromStorageDisk</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'s3'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'/path/to/file'</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Mail</span><span class="token operator">::</span><span class="token function">assertSent</span><span class="token punctuation">(</span><span class="token class-name static-context">OrderShipped</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">OrderShipped</span> <span class="token variable">$mail</span><span class="token punctuation">)</span> <span class="token keyword">use</span> <span class="token punctuation">(</span><span class="token variable">$pdfData</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token variable">$mail</span><span class="token operator">-></span><span class="token function">hasAttachment</span><span class="token punctuation">(</span></span>
<span class="line">        <span class="token class-name static-context">Attachment</span><span class="token operator">::</span><span class="token function">fromData</span><span class="token punctuation">(</span><span class="token keyword">fn</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token variable">$pdfData</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'name.pdf'</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>你可能已经注意到，有 2 种方法可以判断邮件是否发送，即：<code v-pre>assertNotSent</code> 和 <code v-pre>assertNotQueued</code> 。有时你可能希望判断邮件没有被发送或排队。如果要实现这一点，你可以使用 <code v-pre>assertNothingOutgoing</code> 和 <code v-pre>assertNotOutgoing</code> 方法。</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Mail</span><span class="token operator">::</span><span class="token function">assertNothingOutgoing</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Mail</span><span class="token operator">::</span><span class="token function">assertNotOutgoing</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">OrderShipped</span> <span class="token variable">$mail</span><span class="token punctuation">)</span> <span class="token keyword">use</span> <span class="token punctuation">(</span><span class="token variable">$order</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token variable">$mail</span><span class="token operator">-></span><span class="token property">order</span><span class="token operator">-></span><span class="token property">id</span> <span class="token operator">===</span> <span class="token variable">$order</span><span class="token operator">-></span><span class="token property">id</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="邮件和本地开发" tabindex="-1"><a class="header-anchor" href="#邮件和本地开发"><span>邮件和本地开发</span></a></h2>
<p>在开发发送电子邮件的应用程序时，你可能不希望实际将电子邮件发送到实际的电子邮件地址。 Laravel 提供了几种在本地开发期间「禁用」实际发送电子邮件的方法。</p>
<h4 id="日志驱动" tabindex="-1"><a class="header-anchor" href="#日志驱动"><span>日志驱动</span></a></h4>
<p><code v-pre>log</code> 邮件驱动程序不会发送你的电子邮件，而是将所有电子邮件信息写入你的日志文件以供检查。 通常，此驱动程序仅在本地开发期间使用。有关根据环境配置应用程序的更多信息，请查看 <a href="https://learnku.com/docs/laravel/11.x/configurationmd#environment-configuration" target="_blank" rel="noopener noreferrer">配置文档</a>。</p>
<h4 id="helo-mailtrap-mailpit" tabindex="-1"><a class="header-anchor" href="#helo-mailtrap-mailpit"><span>HELO / Mailtrap / Mailpit</span></a></h4>
<p>或者，你可以使用像 <a href="https://usehelo.com/" target="_blank" rel="noopener noreferrer">HELO</a> 或 <a href="https://mailtrap.io/" target="_blank" rel="noopener noreferrer">Mailtrap</a> 这样的服务，并使用 <code v-pre>smtp</code> 驱动将你的电子邮件消息发送到「虚拟」邮箱中，你可以通过在真正的电子邮件客户端中查看它们。这种方法的好处在于，它允许你在 Mailtrap 的消息查看器中实际检查最终的电子邮件。</p>
<p>如果你使用 <a href="https://learnku.com/docs/laravel/11.x/sailmd" target="_blank" rel="noopener noreferrer">Laravel Sail</a>，你可以使用 <a href="https://github.com/axllent/mailpit" target="_blank" rel="noopener noreferrer">Mailpit</a> 预览你的消息。当 Sail 运行时，你可以访问 Mailpit 界面：<code v-pre>http://localhost:8025</code>。</p>
<h4 id="使用全局-to-地址" tabindex="-1"><a class="header-anchor" href="#使用全局-to-地址"><span>使用全局 <code v-pre>to</code> 地址</span></a></h4>
<p>最后，你可以通过调用 <code v-pre>Mail</code> 门面提供的 <code v-pre>alwaysTo</code> 方法来指定一个全局的「收件人」地址。 通常，应该从应用程序的服务提供者之一的 <code v-pre>boot</code> 方法调用此方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Mail</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 引导任何应用程序服务</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">boot</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">void</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token variable">$this</span><span class="token operator">-></span><span class="token property">app</span><span class="token operator">-></span><span class="token function">environment</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'local'</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token class-name static-context">Mail</span><span class="token operator">::</span><span class="token function">alwaysTo</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'taylor@example.com'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="事件" tabindex="-1"><a class="header-anchor" href="#事件"><span>事件</span></a></h2>
<p>Laravel 在发送邮件消息的过程中会触发 2 个事件。<code v-pre>MessageSending</code> 事件在消息发送之前触发，而 <code v-pre>MessageSent</code> 事件在消息发送后触发。请记住，这些事件是在邮件发送时触发的，而不是在排队时触发的。你可以在应用程序中为这些事件创建<a href="https://learnku.com/docs/laravel/11.x/eventsmd" target="_blank" rel="noopener noreferrer">事件监听器</a>:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Mail<span class="token punctuation">\</span>Events<span class="token punctuation">\</span>MessageSending</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token comment">// use Illuminate\Mail\Events\MessageSent;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">LogMessage</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * 处理给定的事件</span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">handle</span><span class="token punctuation">(</span><span class="token class-name type-declaration">MessageSending</span> <span class="token variable">$event</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">void</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token comment">// ...</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="自定义传输" tabindex="-1"><a class="header-anchor" href="#自定义传输"><span>自定义传输</span></a></h2>
<p>Laravel 包含多种邮件传输；但是，你可能希望编写自己的传输程序，通过 Laravel 来发送电子邮件。首先，定义一个扩展 <code v-pre>Symfony\Component\Mailer\Transport\AbstractTransport</code>类。然后，在传输上实现 <code v-pre>doSend</code> 和 <code v-pre>__toString()</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">MailchimpTransactional<span class="token punctuation">\</span>ApiClient</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Symfony<span class="token punctuation">\</span>Component<span class="token punctuation">\</span>Mailer<span class="token punctuation">\</span>SentMessage</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Symfony<span class="token punctuation">\</span>Component<span class="token punctuation">\</span>Mailer<span class="token punctuation">\</span>Transport<span class="token punctuation">\</span>AbstractTransport</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Symfony<span class="token punctuation">\</span>Component<span class="token punctuation">\</span>Mime<span class="token punctuation">\</span>Address</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Symfony<span class="token punctuation">\</span>Component<span class="token punctuation">\</span>Mime<span class="token punctuation">\</span>MessageConverter</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">MailchimpTransport</span> <span class="token keyword">extends</span> <span class="token class-name">AbstractTransport</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * 创建一个新的 Mailchimp 传输实例。</span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">__construct</span><span class="token punctuation">(</span></span>
<span class="line">        <span class="token keyword">protected</span> <span class="token class-name type-declaration">ApiClient</span> <span class="token variable">$client</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword static-context">parent</span><span class="token operator">::</span><span class="token function">__construct</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * <span class="token punctuation">{</span><span class="token keyword">@inheritDoc</span><span class="token punctuation">}</span></span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">protected</span> <span class="token keyword">function</span> <span class="token function-definition function">doSend</span><span class="token punctuation">(</span><span class="token class-name type-declaration">SentMessage</span> <span class="token variable">$message</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">void</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token variable">$email</span> <span class="token operator">=</span> <span class="token class-name static-context">MessageConverter</span><span class="token operator">::</span><span class="token function">toEmail</span><span class="token punctuation">(</span><span class="token variable">$message</span><span class="token operator">-></span><span class="token function">getOriginalMessage</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">        <span class="token variable">$this</span><span class="token operator">-></span><span class="token property">client</span><span class="token operator">-></span><span class="token property">messages</span><span class="token operator">-></span><span class="token function">send</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'message'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'from_email'</span> <span class="token operator">=></span> <span class="token variable">$email</span><span class="token operator">-></span><span class="token function">getFrom</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'to'</span> <span class="token operator">=></span> <span class="token function">collect</span><span class="token punctuation">(</span><span class="token variable">$email</span><span class="token operator">-></span><span class="token function">getTo</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">map</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Address</span> <span class="token variable">$email</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">                <span class="token keyword">return</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'email'</span> <span class="token operator">=></span> <span class="token variable">$email</span><span class="token operator">-></span><span class="token function">getAddress</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'type'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'to'</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line">            <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">all</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'subject'</span> <span class="token operator">=></span> <span class="token variable">$email</span><span class="token operator">-></span><span class="token function">getSubject</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'text'</span> <span class="token operator">=></span> <span class="token variable">$email</span><span class="token operator">-></span><span class="token function">getTextBody</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * 获取传输字符串的表示形式。</span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">__toString</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">string</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token string single-quoted-string">'mailchimp'</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>你一旦定义了自定义传输，就可以通过 <code v-pre>Mail</code> facade提供的 <code v-pre>extend</code> 方法来注册它。通常情况下，这应该在应用程序的 <code v-pre>AppServiceProvider</code> 服务提供商的 <code v-pre>boot</code> 方法中完成。<code v-pre>$config</code> 参数将提供给 <code v-pre>extend</code> 方法的闭包。该参数将包含在应用程序中的<code v-pre>config/mail.php</code> 来配置文件中为 mailer 定义的配置数组。</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Mail<span class="token punctuation">\</span>MailchimpTransport</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Mail</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 启动应用程序服务</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">boot</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">void</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token class-name static-context">Mail</span><span class="token operator">::</span><span class="token function">extend</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'mailchimp'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token keyword type-hint">array</span> <span class="token variable">$config</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">MailchimpTransport</span><span class="token punctuation">(</span><span class="token comment">/* ... */</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>定义并注册自定义传输后，你可以在应用程序中的 <code v-pre>config/mail.php</code> 配置文件中新建一个利用自定义传输的邮件定义：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'mailchimp'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'transport'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'mailchimp'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token comment">// ...</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="额外的-symfony-传输" tabindex="-1"><a class="header-anchor" href="#额外的-symfony-传输"><span>额外的 Symfony 传输</span></a></h3>
<p>Laravel 包括对一些现有的 Symfony 维护的邮件传输的支持，比如 Mailgun 和 Postmark。然而，你可能希望扩展 Laravel 支持其他 Symfony 维护的传输。你可以通过 Composer 安装所需的 Symfony 邮件并在 Laravel 中注册传输来实现这一点。例如，你可以安装并注册 Symfony 的「Brevo」 (原 「Sendinblue」) 邮件：</p>
<div class="language-none line-numbers-mode" data-highlighter="prismjs" data-ext="none" data-title="none"><pre v-pre class="language-none"><code><span class="line">composer require symfony/brevo-mailer symfony/http-client</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>安装好 Brevo 邮件器包后，你可以在应用程序的 <code v-pre>services</code> 配置文件中为你的 Brevo API 凭证添加一个条目：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'brevo'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'key'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'your-api-key'</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>接下来，你可以使用 <code v-pre>Mail</code> facade 的 <code v-pre>extend</code> 方法在 Laravel 中注册传输。通常，这应该在服务提供者的 <code v-pre>boot</code> 方法中完成：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Mail</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Symfony<span class="token punctuation">\</span>Component<span class="token punctuation">\</span>Mailer<span class="token punctuation">\</span>Bridge<span class="token punctuation">\</span>Brevo<span class="token punctuation">\</span>Transport<span class="token punctuation">\</span>BrevoTransportFactory</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Symfony<span class="token punctuation">\</span>Component<span class="token punctuation">\</span>Mailer<span class="token punctuation">\</span>Transport<span class="token punctuation">\</span>Dsn</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 引导任何应用程序服务。</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">boot</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">void</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token class-name static-context">Mail</span><span class="token operator">::</span><span class="token function">extend</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'brevo'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">BrevoTransportFactory</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">create</span><span class="token punctuation">(</span></span>
<span class="line">            <span class="token keyword">new</span> <span class="token class-name">Dsn</span><span class="token punctuation">(</span></span>
<span class="line">                <span class="token string single-quoted-string">'brevo+api'</span><span class="token punctuation">,</span></span>
<span class="line">                <span class="token string single-quoted-string">'default'</span><span class="token punctuation">,</span></span>
<span class="line">                <span class="token function">config</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'services.brevo.key'</span><span class="token punctuation">)</span></span>
<span class="line">            <span class="token punctuation">)</span></span>
<span class="line">        <span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>一旦你的传输被注册，你可以在应用程序的 <code v-pre>config/mail.php</code> 配置文件中创建一个利用新传输的邮件定义：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'brevo'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'transport'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'brevo'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token comment">// ...</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p>本译文仅用于学习和交流目的，转载请务必注明文章译者、出处、和本文链接<br>
我们的翻译工作遵照 <a href="https://learnku.com/docs/guide/cc4.0/6589" target="_blank" rel="noopener noreferrer">CC 协议</a>，如果我们的工作有侵犯到您的权益，请及时联系我们。</p>
</blockquote>
<hr>
<blockquote>
<p>原文地址：<a href="https://learnku.com/docs/laravel/11.x/mailmd/16682" target="_blank" rel="noopener noreferrer">https://learnku.com/docs/laravel/11.x/ma...</a></p>
<p>译文地址：<a href="https://learnku.com/docs/laravel/11.x/mailmd/16682" target="_blank" rel="noopener noreferrer">https://learnku.com/docs/laravel/11.x/ma...</a></p>
</blockquote>
</div></template>


